package com.jiyici.view;

import java.util.ArrayList;

import jiyici.Constant.ItemParamType;
import jiyici.ParamType;
import android.app.DatePickerDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jiyici.R;
import com.jiyici.controller.DataManager;
import com.jiyici.model.Category;
import com.jiyici.model.Item;
import com.jiyici.model.ItemParam;
import com.jiyici.model.Record;
import com.jiyici.model.RecordParam;
import com.jiyici.utils.StringUtils;
import com.jiyici.view.DialogWrapper.DialogAction;

public class DialogUtil {
    
    public static void showConfirmDialog(final Context context, String title, String msg, DialogAction doneAction) {
        final DialogWrapper dialogWrapper = new DialogWrapper(context);
        dialogWrapper.setTitle(title);
        dialogWrapper.setContentView(R.layout.dialogbody_confirm);
        final TextView txt_confirm = (TextView) dialogWrapper.getContentView().findViewById(R.id.txt_confirm);
        txt_confirm.setText(msg);
        dialogWrapper.setAction(R.string.dialog_yes, doneAction, R.string.dialog_cancel, null);
        dialogWrapper.show();
    }
    
    public static void showNewCategoryDialog(final Context context) {
        final DialogWrapper dialogWrapper = new DialogWrapper(context);
        dialogWrapper.setTitle(R.string.new_category);
        dialogWrapper.setContentView(R.layout.dialogbody_add_category);
        final EditText edit_category_name = (EditText) dialogWrapper.getContentView().findViewById(
                R.id.edit_category_name);
        edit_category_name.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                dialogWrapper.getDoneButton().setEnabled(!StringUtils.isBlank(s.toString()));
            }
        });

        edit_category_name.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (EditorInfo.IME_ACTION_DONE == actionId) {
                    String categoryName = edit_category_name.getText().toString();
                    if (StringUtils.isBlank(categoryName))
                        return false;
                    categoryName = StringUtils.trim(categoryName);
                    Category category = new Category();
                    category.name = categoryName;
                    DataManager.addCategory(category);
                    dialogWrapper.dismiss();
                    return true;
                }
                return false;
            }
        });

        DialogAction doneAction = new DialogAction() {

            @Override
            public int run() {
                String categoryName = edit_category_name.getText().toString();
                categoryName = StringUtils.trim(categoryName);
                if (StringUtils.isBlank(categoryName))
                    return NOOP;
                Category category = new Category();
                category.name = categoryName;
                DataManager.addCategory(category);
                return DISMISS;
            }
        };
        dialogWrapper.setAction(R.string.dialog_done, doneAction, R.string.dialog_cancel, null);
        dialogWrapper.setBtnDoneInitailDisabled(true);
        dialogWrapper.show();
    }
    
    public static void showAddOneDialog(final Context context, final Item item) {
        DialogWrapper dialogWrapper = new DialogWrapper(context);
        dialogWrapper.setTitle(item.itemName);
        dialogWrapper.setContentView(R.layout.dialogbody_record_item);
        View contentView = dialogWrapper.getContentView();
        View timeInput = contentView.findViewById(R.id.record_time);
        timeInput.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                displayTimePickerDialog(context);
            }
        });
        
        final ViewGroup property_input_layout = (ViewGroup) contentView.findViewById(R.id.property_input_layout);
        final int propertySize = item.params.size();
        for (int i = 0; i < propertySize; i++) {
            ItemParam param = item.params.get(i);
            View propertyInput = LayoutInflater.from(context).inflate(R.layout.view_property_input, property_input_layout, false);
            TextView propertyName = (TextView) propertyInput.findViewById(R.id.property_name);
            EditText propertyEdit = (EditText) propertyInput.findViewById(R.id.property_edit);
            String hint = context.getString(ParamType.getParamType(param.paramType).typeHint);
            if (param.isMust) {
                hint = "*" + hint;
            }
            propertyEdit.setHint(hint);
            int inputtype = EditorInfo.TYPE_CLASS_TEXT;
            if (param.paramType == ItemParamType.NUMBER) {
                inputtype = EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_DECIMAL | EditorInfo.TYPE_NUMBER_FLAG_SIGNED;
            }
            propertyEdit.setInputType(inputtype);
            propertyName.setText(param.itemParamName);
            propertyInput.setTag(param);
            property_input_layout.addView(propertyInput);
        }
        
        DialogAction doneAction = new DialogAction() {
            
            @Override
            public int run() {
                /* check data input */
                Record record = new Record();
                record.itemId = item.getId();
                record.recordTime = System.currentTimeMillis();
                ArrayList<RecordParam> params = new ArrayList<RecordParam>(propertySize + 1);
                for (int i = 0; i < propertySize; i++) {
                    View propertyInput = property_input_layout.getChildAt(i);
                    ItemParam param = (ItemParam) propertyInput.getTag();
                    EditText propertyEdit = (EditText) propertyInput.findViewById(R.id.property_edit);
                    String value = propertyEdit.getText().toString().trim();
                    if (param.isMust && StringUtils.isBlank(value)) {
                        propertyEdit.requestFocus();
                        return NOOP;
                    }
                    RecordParam recordParam = new RecordParam();
                    recordParam.itemParamId = param.getId();
                    recordParam.value = value;
                    params.add(recordParam);
                }
                DataManager.saveRecord(record, params);
                
                return DISMISS;
            }
        };
        dialogWrapper.setAction(R.string.add_one, doneAction, R.string.dialog_cancel, null);
        dialogWrapper.show();
    }
    
    private static void displayTimePickerDialog(final Context context) {
        DatePickerDialog dpd = new DatePickerDialog(context, null, 1980, 06, 02);
        dpd.show();
    }
    
    public static class Notify {
        
        public static final Notify NOTIFY_WHEN_DELETE_PROPERTY = new Notify(1, R.string.dialog_nofity_delete_property);
        
        public static final Notify NOTIFY_WHEN_TRY_TO_CHANGE_PARAM_TYPE = new Notify(2, R.string.dialog_nofity_change_param_type);
        
        private int notifyType;
        private int messageResId;
        
        private Notify(int notifyType, int messageResId) {
            super();
            this.notifyType = notifyType;
            this.messageResId = messageResId;
        }
        public int getNotifyType() {
            return notifyType;
        }
        public int getMessageResId() {
            return messageResId;
        }
        
    }
    
    public static void showNofity(final Context context, final Notify notify) {
        if (!DataManager.getShowNotifyDialog(notify))
            return;
        final DialogWrapper dialogWrapper = new DialogWrapper(context);
        dialogWrapper.setContentView(R.layout.dialogbody_delete_property_notify);
        TextView tv = (TextView)dialogWrapper.getContentView().findViewById(R.id.notify_message);
        tv.setText(notify.getMessageResId());
        final CheckBox cb_clear_data = (CheckBox) dialogWrapper.getContentView().findViewById(R.id.cb_clear_data);
        DialogAction doneAction = new DialogAction() {

            @Override
            public int run() {
                boolean clearData = cb_clear_data.isChecked();
                if (clearData)
                    DataManager.setShowNotifyDialog(notify, false);
                return DISMISS;
            }
        };
        dialogWrapper.setAction(R.string.dialog_ok, doneAction);
        dialogWrapper.show();
    }
}
